package com.example.zzl.adapter.SearchFragment;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.example.zzl.R;
import com.example.zzl.app.MyApplication;
import com.example.zzl.model.bean.SearchFragment.CateData;
import com.example.zzl.utils.LruImageCache;

import java.util.ArrayList;

/**
 * Created by zhang on 2017/2/11.
 */

public class GvCataAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<CateData.ResultBean> list;
    private ImageLoader imageLoader;

    public GvCataAdapter(Context context, ArrayList<CateData.ResultBean> list) {
        this.context = context;
        this.list = list;
        imageLoader = new ImageLoader(MyApplication.queue, LruImageCache.getInstance());
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder vh;
        if(view == null){
            view = View.inflate(context, R.layout.gv_cate_item,null);
            vh = new ViewHolder();
            vh.iv = (ImageView)view.findViewById(R.id.iv_gv_cate);
            vh.tv = (TextView)view.findViewById(R.id.tv_cate);
            view.setTag(vh);

        }else{
            vh = (ViewHolder)view.getTag();
        }
        if(list.get(i).getCatName() == null){
            vh.iv.setImageBitmap(BitmapFactory.decodeResource(context.getResources(),R.drawable.icon_allcat));
            vh.tv.setText("全部");
        }else {
            vh.tv.setText(list.get(i).getCatName());
            loadImgByVolley(list.get(i).getCatImage(), vh.iv);
        }
        return view;
    }

    class ViewHolder{
        ImageView iv;
        TextView tv;
    }

    private void loadImgByVolley(String string, ImageView ivImage) {
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(ivImage,
                R.drawable.ic_launcher, R.drawable.ic_launcher);
        imageLoader.get(string, listener);
    }
}
